
*For Margaret dropbox folders 
global NCREIF "/Users/BeckaBrolinson/Dropbox/NCREIF/data" 
global build 	"$NCREIF/build" 
global analysis "$NCREIF/analysis"
global results 	"$analysis/results"
global figures 	"$analysis/figures" 


*------------------------------------------------------------------------------*
*	Step 17- Heterogenous TE Robustness for statitistical differences	   *
*------------------------------------------------------------------------------*	


	*Het effects by energy star score 
	set more off 
	forvalues i=2/3{
	use "$build/FakeNCREIFAnnualizedDatawcontrol_postmatch`i'_wmatches.dta", clear
	
	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 
	
	*Generate Variable for Intensity of Treatment 
	bysort propnum: gen rate=everrated if year>=firstyearrated
	*Generate different bins for rating 	
	gen rate1=1 if rate>=75 & rate<81 
	replace rate1=0 if missing(rate1)
	gen rate2=1 if rate>=81 & rate<100 
	replace rate2=0 if missing(rate2)                                                                                              
	*Generate a catogory for these so that I can plot them 
	
	gen rate_cat=. 
	replace rate_cat=1 if rate1==1 
	replace rate_cat=2 if rate2==1 
	
*Diff in diff regression with Het treatment effects 

	set more off 
	*List of covariates 
	#d ;
	global Cov0 	""; 
	global Cov1 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s
	 percentleased real_capex_ti_sqft real_capex_bldimp_sqft "; 
	global Cov2 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s
	 age age2 percentleased used_space1000s
	dFundType1 dFundType2 dFundType4 dFundType5 dFundType6";
	global Cov3	"Covered_E real_elecprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s"; 
	#d cr 
	
	*Generate the heterogenous treatment effects interaction 
	gen interaction_1 = treat*post*rate1 
	gen interaction_2 = treat*post*rate2 

	*Testing whether there are statistically different treatment effects between the high and low scores 
	eststo UtilFECov1_het_test: reghdfe logrealutilpersf interaction interaction_2 rl_yr_rentpersf $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  UtilFECov1_het_test, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	eststo RentFECov1_het_test: reghdfe logrealrentpersf interaction interaction_2 $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  RentFECov1_het_test, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")

	label var interaction "Cert*Post" 
	label var interaction_1 "1(Cert*Post*\$<=\$Median)"
	label var interaction_2 "1(Cert*Post*\$>\$Median)"
	label var treat "1[treat=1]"
	label var logrealrentpersf "ln(Rent/Sq. Ft.) (\\$)"
	label var Covered_E "Benchmarking Law" 
	label var real_elecprice "Avg. Elec. Price (\\$/MWh)" 
	label var real_gasprice "Avg. Gas Price (\\$/Mcf)" 
	label var Unemployment "Unemployment" 
	label var age "Building Age" 
	label var age2 "Building Age Squared" 
	label var used_space "Used Space (Pct. Leased * Sq. Ft.)"
	label var used_space1000s "Used Space (Pct. Leased * Sq. Ft.) (1000s)"
	labe var percentleased "Percent Leased (\%)"
	label var real_capex_ti "Cap Exp. (Tenant Imp.)" 
	label var real_capex_ti_sqft "Cap Exp./Sq. Ft. (Tenant Imp.) (\\$)"
	label var real_capex_bldimp "Cap Exp. (Bldg. Imp.)"
	label var real_capex_bldimp_sqft "Cap Exp./Sq. Ft. (Bldg. Imp.) (\\$)"
	label var HDD "HDD" 
	label var HDD1000s "HDD (1000s)" 
	label var CDD "CDD" 
	label var CDD1000s "CDD (1000s)" 
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var logrealutilpersf "ln(Util/Sq. Ft.) (\\$)"
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var rl_yr_utilpersf "Util. per Sq. Ft."
	label var yrbuilt "Year Built" 
	label var sqft "Square Feet"
	label var sqft1000s "Square Feet (1000s)" 

	*make Latex Table 
	esttab RentFECov1_het_test UtilFECov1_het_test using "$results/06_diffindiff_het_matched`i'_test.tex" , label replace booktabs ///
	alignment(SSSS) ///
	b(%12.3f) se(%12.3f) star(* 0.05) /// sets format of parameters, standard errors, and stars
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	nonotes nogaps /// removes notes from bottom of table 
	indicate(`r(indicate_fe)', label("Y" "")) /// adds y & N for FE inclusions
	title(Matched Sample: Heterogeneous Treatment Effects by Energy Star Score \label{appendixtab04heteffectsfmatched`i'}) ///
	addnote({\scriptsize * p$<$0.05. The standard errors reported in parenthesis have been clustered at the CBSA level.})

	
	}
	
	
	
**Het effects by square footage 
	set more off 
	forvalues i=2/3{
	use "$build/FakeNCREIFAnnualizedDatawcontrol_postmatch`i'_wmatches.dta", clear
	*median square footage is 218254
	
	
	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 

	*Generate different bins for rating 	
	gen sqft1=(sqft<=218254) 
	gen sqft2=(sqft>218254) 

	
	*Generate a catogory for these so that I can plot them 
	gen sqft_cat=. 
	replace sqft_cat=1 if sqft1==1 
	replace sqft_cat=2 if sqft2==1 
	
*Diff in diff regression with Het treatment effects 

	set more off 
	*List of covariates 
	#d ;
	global Cov0 	""; 
	global Cov1 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s
	 percentleased real_capex_ti_sqft real_capex_bldimp_sqft "; 
	global Cov2 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s
	 age age2 percentleased used_space1000s
	dFundType1 dFundType2 dFundType4 dFundType5 dFundType6";
	global Cov3	"Covered_E real_elecprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s"; 
	#d cr 
	
	*Generate the heterogenous treatment effects interaction 
	gen interaction_1 = treat*post*sqft1 
	gen interaction_2 = treat*post*sqft2 
	*Diff in diff 
	
	eststo UtilFECov1_het: reghdfe logrealutilpersf interaction interaction_2 rl_yr_rentpersf $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  UtilFECov1_het, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	eststo RentFECov1_het: reghdfe logrealrentpersf interaction interaction_2 $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  RentFECov1_het, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")

	label var interaction "Cert*Post" 
	label var interaction_1 "1(Cert*Post*\$<= \$ Median)"
	label var interaction_2 "1(Cert*Post* \$ > \$ Median)"
	label var treat "1[treat=1]"
	label var logrealrentpersf "ln(Rent/Sq. Ft.) (\\$)"
	label var Covered_E "Benchmarking Law" 
	label var real_elecprice "Avg. Elec. Price (\\$/MWh)" 
	label var real_gasprice "Avg. Gas Price (\\$/Mcf)" 
	label var Unemployment "Unemployment" 
	label var age "Building Age" 
	label var age2 "Building Age Squared" 
	label var used_space "Used Space (Pct. Leased * Sq. Ft.)"
	label var used_space1000s "Used Space (Pct. Leased * Sq. Ft.) (1000s)"
	labe var percentleased "Percent Leased (\%)"
	label var real_capex_ti "Cap Exp. (Tenant Imp.)" 
	label var real_capex_ti_sqft "Cap Exp./Sq. Ft. (Tenant Imp.) (\\$)"
	label var real_capex_bldimp "Cap Exp. (Bldg. Imp.)"
	label var real_capex_bldimp_sqft "Cap Exp./Sq. Ft. (Bldg. Imp.) (\\$)"
	label var HDD "HDD" 
	label var HDD1000s "HDD (1000s)" 
	label var CDD "CDD" 
	label var CDD1000s "CDD (1000s)" 
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var logrealutilpersf "ln(Util/Sq. Ft.) (\\$)"
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var rl_yr_utilpersf "Util. per Sq. Ft."
	label var yrbuilt "Year Built" 
	label var sqft "Square Feet"
	label var sqft1000s "Square Feet (1000s)"
	
	*make Latex Table 
	esttab RentFECov1_het UtilFECov1_het using "$results/08_diffindiff_hetsqft_matched`i'_test.tex" , label replace booktabs ///
	alignment(SSSS) ///
	b(%12.3f) se(%12.3f) star(* 0.05) /// sets format of parameters, standard errors, and stars
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	nonotes nogaps /// removes notes from bottom of table 
	indicate(`r(indicate_fe)', label("Y" "")) /// adds y & N for FE inclusions
	title(Matched Sample: Heterogeneous Treatment Effects by Square Footage \label{appendixtab05heteffectssqftfmatched`i'test}) ///
	addnote({\scriptsize * p$<$0.05. The standard errors reported in parenthesis have been clustered at the CBSA level.})

	}
	
	*Het effects by building age
	set more off 
	forvalues i=2/3{
	use "$build/FakeNCREIFAnnualizedDatawcontrol_postmatch`i'.dta", clear	
	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 

	*Generate different bins for rating 	
	gen age1=(age<=19) 
	gen age_2=(age>19) 

	
*Diff in diff regression with Het treatment effects 

	set more off 
	*List of covariates 
	#d ;
	global Cov0 	""; 
	global Cov1 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s
	 percentleased real_capex_ti_sqft real_capex_bldimp_sqft "; 
	global Cov2 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s
	 age age2 percentleased used_space1000s
	dFundType1 dFundType2 dFundType4 dFundType5 dFundType6";
	global Cov3	"Covered_E real_elecprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s"; 
	#d cr 
	
	*Generate the heterogenous treatment effects interaction 
	gen interaction_1 = treat*post*age1 
	gen interaction_2 = treat*post*age_2 
	*Diff in diff 
	
	eststo UtilFECov1_het: reghdfe logrealutilpersf interaction interaction_2 rl_yr_rentpersf $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  UtilFECov1_het, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	eststo RentFECov1_het: reghdfe logrealrentpersf interaction interaction_2 $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  RentFECov1_het, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")

	label var interaction "Cert*Post" 
	label var interaction_1 "1(Cert*Post*\$<= \$ Median)"
	label var interaction_2 "1(Cert*Post* \$ > \$ Median)"
	label var treat "1[treat=1]"
	label var logrealrentpersf "ln(Rent/Sq. Ft.) (\\$)"
	label var Covered_E "Benchmarking Law" 
	label var real_elecprice "Avg. Elec. Price (\\$/MWh)" 
	label var real_gasprice "Avg. Gas Price (\\$/Mcf)" 
	label var Unemployment "Unemployment" 
	label var age "Building Age" 
	label var age2 "Building Age Squared" 
	label var used_space "Used Space (Pct. Leased * Sq. Ft.)"
	label var used_space1000s "Used Space (Pct. Leased * Sq. Ft.) (1000s)"
	labe var percentleased "Percent Leased (\%)"
	label var real_capex_ti "Cap Exp. (Tenant Imp.)" 
	label var real_capex_ti_sqft "Cap Exp./Sq. Ft. (Tenant Imp.) (\\$)"
	label var real_capex_bldimp "Cap Exp. (Bldg. Imp.)"
	label var real_capex_bldimp_sqft "Cap Exp./Sq. Ft. (Bldg. Imp.) (\\$)"
	label var HDD "HDD" 
	label var HDD1000s "HDD (1000s)" 
	label var CDD "CDD" 
	label var CDD1000s "CDD (1000s)" 
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var logrealutilpersf "ln(Util/Sq. Ft.) (\\$)"
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var rl_yr_utilpersf "Util. per Sq. Ft."
	label var yrbuilt "Year Built" 
	label var sqft "Square Feet"
	label var sqft1000s "Square Feet (1000s)"
	
	*make Latex Table 
	esttab RentFECov1_het UtilFECov1_het using "$results/14_diffindiff_hetage_matched`i'_test.tex" , label replace booktabs ///
	alignment(SSSS) ///
	b(%12.3f) se(%12.3f) star(* 0.05) /// sets format of parameters, standard errors, and stars
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	nonotes nogaps /// removes notes from bottom of table 
	indicate(`r(indicate_fe)', label("Y" "")) /// adds y & N for FE inclusions
	title(Matched Sample: Heterogeneous Treatment Effects by Building Age\label{appendixtab06heteffectsagefmatched`i'}) ///
	addnote({\scriptsize * p$<$0.05. The standard errors reported in parenthesis have been clustered at the CBSA level.})

	}
	
	
	
	
	
